package org.hqs.clinic.service;

import org.hqs.clinic.mapper.ClinicMapper;
import org.hqs.clinic.pojo.Clinic;
import org.hqs.feign.clients.DepartmentClient;
import org.hqs.feign.pojo.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClinicService {
    @Autowired
    private ClinicMapper clinicMapper;

    @Autowired
    private DepartmentClient departmentClient;

    public Clinic queryClinicById(Long clinicId) {
        Clinic clinic = clinicMapper.findById(clinicId);
        // 找出科室
        Department department = departmentClient.getDepartmentById(Long.valueOf(clinic.getDepartmentId())).getData();
        //放入门诊
        clinic.setDepartment(department);
        // 返回
        return clinic;
    }

    public List<Clinic> findAllClinics() {
        List<Clinic> clinics = clinicMapper.findAllClinics();
        for (int i = 0; i < clinics.size(); i++) {
            Clinic clinic = clinics.get(i);
            Department department = departmentClient.getDepartmentById(Long.valueOf(clinic.getDepartmentId())).getData();
            clinic.setDepartment(department);
            clinics.set(i , clinic);
        }
        return clinics;
    }

    public int addClinic(Clinic clinic) {
        return clinicMapper.insertClinic(clinic);
    }

    public int updateClinic(Clinic clinic) {
        return clinicMapper.updateClinic(clinic);
    }

    public int deleteClinicById(Long clinicId) {
        return clinicMapper.deleteClinicById(clinicId);
    }

    public List<Clinic> findClinicByDepartmentId(Long departmentId) {
       List<Clinic> clinics = clinicMapper.findClinicByDepartmentId(departmentId);
        for (int i = 0; i < clinics.size(); i++) {
            Clinic clinic = clinics.get(i);
            Department department = departmentClient.getDepartmentById(Long.valueOf(clinic.getDepartmentId())).getData();
            clinic.setDepartment(department);
            clinics.set(i , clinic);
        }
        return clinics;
    }
}
